<template>
	<view class="page">
		<scroll-view scroll-x="true" class="cate-wrap">
		  <view class="cate-item" v-for="item in cotegory">
			  <view class="fcc" @click='selected(item.category_id)'>
				  <view class="f30 c6" :class="category_id == item.category_id?'c3 fb':'c6'">{{item.name}}</view>
				  <view class="line" :class="category_id == item.category_id?'line-active':''"></view>
			  </view>
		  </view>
		</scroll-view>
		
		<view class="source-wrap">
			<view class="source-item" v-for="item in list" @click="$comm.onPage(`/pages/sourceCenter/sourceDetail?category_id=${item.category_id}&material_id=${item.material_id}`)">
				<view class="user-box">
					<view class="user-msg">
						<image :src="item.avatar.file_path"></image>
						{{item.nickName}}
					</view>
					<view class="say-date">{{item.create_time}}</view>
				</view>
				<view class="content">
					{{item.material_title}}
				</view>
				<view class="image-wrap" v-if="item.material_type==10">
					<!-- @click="preImg(item.image,i)" -->
					<image :src="img.file_path" v-for="(img,i) in item.image" ></image>
				</view>
				
				<view v-if="item.material_type==20">
					<video :src="item.video.file_path" :muted="true"></video>
				</view>
				
				<!-- #ifndef H5 -->
					<!-- <view class="handle-wrap">
						<view class="fcc" @click="copyText(item.material_title)">
							<image src="../../static/copy-icon.png"></image>
							复制文案
						</view>
						<view class="fcc" v-if="item.image.length!=0 || item.video.file_path!=''" @click="saveFile(item.image,item.video.file_path)">
							<image src="../../static/save-icon.png"></image>
							一键保存
						</view>
					</view> -->
				<!-- #endif -->
				
			</view>
			<!-- </navigator> -->
			
			<view class="null-tis" v-if="list.length==0">
				暂无数据~
			</view>
		</view>
		
		
		<!-- <navigator hoverClass="none" openType="navigate" url="/pages/source/release">
			<image class="release-wrap" src="../../static/images/say-icon.png"></image>
		</navigator> -->
		
		
		<view class="release-wrap">
			<view class="release-box">
				<!-- <image bindtap="chooseAdd" src="/static/images/say-icon.png" style="position: absolute;right: 0rpx;top: 10rpx;height: 123rpx;"></image> -->
				<navigator hoverClass="none" openType="navigate" url="/pages/source/release?is_image=1">
					<view v-if="showChooseAddType" class="releaseI">发布图片</view>
				</navigator>
				<navigator hoverClass="none" openType="navigate" url="/pages/source/release?is_image=0">
					<view v-if="showChooseAddType" class="releaseV">发布视频</view>
				</navigator>
			</view>
		</view>
		
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				category_id:'',
				cotegory:[],
				list:[],
				page: 1,
				show: 0,
				getmore: 1,
				showChooseAddType:false
			}
		},
		methods: {
				chooseAdd(){
					this.showChooseAddType = !this.showChooseAddType
				},
				saveFile(img,video){
					// 保存图片
					if(img.length!=0){
						for(let i=0;i<img.length;i++){
							uni.showLoading({
								title:`正在保存图片${i}`
							})
							uni.downloadFile({
							    url: `${img[i].file_path}`, //仅为示例，并非真实的资源
							    success: (res) => {
									uni.saveImageToPhotosAlbum({
									    filePath: res.tempFilePath,
									    success: function (res) {
											uni.hideLoading()
											uni.showToast({
												title:`图${i}保存成功`
											})
									    },
										fail() {
											uni.hideLoading()
											uni.showToast({
												title:`图${i}保存失败`,
												icon:'none'
											})
										}
									});
								},
								complete(e) {
									console.log(e)
								}
							});
						}
					// 保存视频
					}else if(video!=''){
						uni.showLoading({
							title:"正在保存视频",
							icon:"none"
						})
						uni.downloadFile({
						    url: `${video}`, //仅为示例，并非真实的资源
						    success: (res) => {
								console.log(res)
								uni.saveFile({
								  tempFilePath: res.tempFilePath,
								  success (res) {
									console.log(res)
									uni.hideLoading()
									uni.showToast({
										title:"保存视频成功"
									})
								  },
								  fail() {
									console.log(res)
								  	uni.hideLoading()
									uni.showToast({
										title:"保存视频失败",
										icon:"none"
									})
								  }
								})
							}
						});
					}else{
						
					}
				},
			
			  /**
			   * 生命周期函数--监听页面加载
			   */
			  onLoad: function (options) {
			    this.getcategory()
				this.getList()
			  },
			  
			  getcategory: function(){
			    var _this = this
			    _this.loading = true
			    _this._get(
			    	'plus.material.material/category',
			    	{},
			    	function(res) {
						let category = res.data.category
						category.unshift({
							category_id: '',
							create_time: '',
							name: "全部",
						})
			    		_this.cotegory = category
			    		uni.hideLoading()
			    	}
			    )
			  },
			  
			  
			  
			  getList(){
				  var _this = this;
				  _this.loading = true;
				  _this._get(
				  	'plus.material.material/index',
				  	{
						 category_id:_this.category_id?_this.category_id:'',
						 page:_this.page
				  	},
				  	function(res) {
				  		console.log(res)
						if(_this.page==1){
							_this.list = res.data.list.data
						}else{
							if(res.data.list.data.length==0){
								uni.showToast({
									title:"没有更多了",
									icon:"none"
								})
							}else{
								_this.list = [..._this.list,...res.data.list.data]
							}
						}
				  		uni.hideLoading();
				  	}
				  );
			  },
			  
			  selected: function (id) {
			    this.category_id = id
			    this.list = []
			    this.page = 1
			    this.getList();
			  },
			  // 预览图片
			  preImg(list,index){
				  let urls = []
				  for(let item of list){
					  urls.push(item.file_path)
				  }
				  
				 uni.previewImage({
				 	urls:urls,
					current:index
				 })
			  },
			  // 复制文案
			  copyText(e){
				 uni.setClipboardData({
				     data: e,
				     success: function () {
				         console.log('success');
				     }
				 })
			  },
		},
		onReachBottom() {
			this.page+=1
			this.getList()
		}
	}
</script>

<style>
	/* pages/source/index.wxss */
	.source-wrap{
		/* #ifdef MP */
		padding-top: 100rpx;
		/* #endif */
		/* #ifdef H5 */
		padding-top: 30rpx;
		/* #endif */
	}
	.source-item{
		width:710rpx;
		border-radius:16rpx;
		margin: 20rpx;
		background-color: #ffffff;
		padding: 30rpx;
		box-sizing: border-box;
	}
	.source-item>.user-box{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.source-item>.user-box>.user-msg{
		display: flex;
		align-items: center;
		font-size: 30rpx;
		color: #999999;
	}
	.source-item>.user-box>.user-msg>image{
		width: 70rpx;
		height: 70rpx;
		border-radius: 50%;
		margin-right: 16rpx;
	}
	.source-item>.user-box>.say-date{
		font-size: 22rpx;
		color: #999;
	}
	.source-item>.content{
		font-size: 30rpx;
		color: #666666;
		font-weight:400;
		line-height:42rpx;
		margin: 18rpx 0rpx;
	}
	.source-item>.image-wrap{
		display: flex;
		flex-wrap: wrap;
	}
	.source-item>.image-wrap>image{
		width: 212rpx;
		height: 212rpx;
		border-radius: 10rpx;
		margin-right: 6rpx;
		margin-top: 6rpx;
	}
	.source-item>.image-wrap>image:nth-child(3n) {
		margin-right: 0rpx;
	}
	.source-item>.handle-wrap{
		display: flex;
		justify-content: flex-end;
	}
	.source-item>.handle-wrap>view{
		font-size: 24rpx;
		color: #333333;
		margin: 30rpx 0rpx 0rpx 46rpx;
	}
	.source-item>.handle-wrap>view>image{
		width: 28rpx;
		height: 28rpx;
		margin-right: 8rpx;
	}
	.release-wrap{
		position: fixed;
		right: 0;
		top: 750rpx;
		height: 118rpx;
		width: 130rpx;
	}
	.release-box{
		height: 100%;
		width: 100%;
		position: relative;
	}
	.release-box view{
		height: 60rpx;
		width: 60rpx;
		border-radius: 50%;
		position: absolute;
		background-color: rgb(200,20,43);
		color: #FFF;
		font-size: 22rpx;
		text-align: center;
		padding: 6rpx;
	}
	.releaseI{
		 top: -15rpx;
		 left: -10rpx;
	}
	.releaseV{
		bottom: -15rpx;
		left: -10rpx;
	}
	
	.cate-wrap{
		height:100rpx;
		width: 750rpx;
		position: fixed;
		top: 0;
		z-index: 1000;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #EEE;
		white-space: nowrap;
		padding-left: 10rpx;
		border-radius: 0rpx 0rpx 16rpx 16rpx;
		box-sizing: border-box;
	}
	.cate-item{
		height: 100%;
		padding: 0rpx 20rpx;
		display: inline-block;
		position: relative;
	}
	.cate-item>view{
		height: 100%;
		width: 100%;
	}
	.cate-item .line{
		position: absolute;
		bottom: 16rpx;
		width: 74rpx;
		height: 4rpx;
	}
	.line-active{
		background-color: #EE1414;
	}
	.fcc{
		display: flex;
		align-items: center;
		justify-content: center; 
	}
	.null-tis{
		height: 100rpx;
		color: #666;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.c6{
		color: #666;
	}
	.c3{
		color: #333;
	}
</style>
